package com.lovo.project.servlet.product;

import com.lovo.project.enums.ProductEnum;
import com.lovo.project.exception.OurException;
import com.lovo.project.model.ProductDO;
import com.lovo.project.service.ProductService;
import com.lovo.project.service.impl.ProductServiceImpl;
import com.lovo.project.vo.ResultVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @title 商品详细信息url
 * @author: chenqiang
 * @date: 2020/11/4 14:09
 */
@WebServlet(name = "ProductDetailsServlet",value = "/product/productDetails")
public class ProductDetailsServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html; charset=UTF-8");
            ProductService productService =new ProductServiceImpl();
            Integer productId =Integer.parseInt(request.getParameter("productId"));

            PrintWriter out = response.getWriter();
            try {
           ProductDO productDO =productService.findById(productId);
            out.print(ResultVO.success().add(productDO).toJson());
             } catch (OurException e) {
            out.print(ResultVO.fail(e.getCodeEnum()).toJson());
            }catch (Exception e1){
            out.print(ResultVO.fail(ProductEnum.PRODUCT_Find_ENUM).toJson());
            }finally {
            out.close();
            }
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
